package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.ZsyyServicePeople;
import com.ruoyi.system.mapper.ZsyyServicePeopleMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyOrderAddBo;
import com.ruoyi.system.bo.ZsyyOrderQueryBo;
import com.ruoyi.system.bo.ZsyyOrderEditBo;
import com.ruoyi.system.domain.ZsyyOrder;
import com.ruoyi.system.mapper.ZsyyOrderMapper;
import com.ruoyi.system.vo.ZsyyOrderVo;
import com.ruoyi.system.service.IZsyyOrderService;
import com.ruoyi.system.mapper.ZsyyOrderMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 服务订单管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyOrderServiceImpl extends ServicePlusImpl<ZsyyOrderMapper, ZsyyOrder> implements IZsyyOrderService {
    @Autowired
    private ZsyyOrderMapper iZsyyOrderMapper;
    @Autowired
    private ZsyyServicePeopleMapper servicePeopleMapper;

    @Override
    public ZsyyOrderVo queryById(Long id){
        return iZsyyOrderMapper.getInfo(id);
    }

    @Override
    public TableDataInfo<ZsyyOrderVo> queryPageList(ZsyyOrderQueryBo bo) {
		Page<ZsyyOrderVo> page = iZsyyOrderMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyOrderVo> queryList(ZsyyOrderQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyOrderVo.class);
    }

    private LambdaQueryWrapper<ZsyyOrder> buildQueryWrapper(ZsyyOrderQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getOrderNo()), ZsyyOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getStatus() != null, ZsyyOrder::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getPayChannel()), ZsyyOrder::getPayChannel, bo.getPayChannel());
        lqw.eq(bo.getPayType() != null, ZsyyOrder::getPayType, bo.getPayType());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyOrder::getPhone, bo.getPhone());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyOrderAddBo bo) {
        ZsyyOrder add = BeanUtil.toBean(bo, ZsyyOrder.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyOrderEditBo bo) {
    	if (!CommonUtil.isEmpty(bo.getPeopleId())){
			ZsyyServicePeople people = servicePeopleMapper.selectById(bo.getPeopleId());
			people.setStatus(1);//设置为服务中
			servicePeopleMapper.updateById(people);
		}
		bo.setStatus(6);	//修改服务状态为服务进行中
        ZsyyOrder update = BeanUtil.toBean(bo, ZsyyOrder.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    //完成服务
    @Override
    public Boolean serviceOK(Long id) {
    	ZsyyOrder order = iZsyyOrderMapper.selectById(id);
    	if (!CommonUtil.isEmpty(order.getPeopleId())){
			ZsyyServicePeople people = servicePeopleMapper.selectById(order.getPeopleId());
			people.setStatus(0);//设置为空闲中
			servicePeopleMapper.updateById(people);
		}
		order.setStatus(5);	//修改服务状态为已完成
        return updateById(order);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
